package com.wanmait.myhouse.filter;

import com.wanmait.myhouse.vo.Mediator;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/mediator/*")
public class MediatorBanLogin implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String uri = httpServletRequest.getRequestURI();
        HttpSession session = httpServletRequest.getSession();
        Object o = session.getAttribute("mediator");
        Mediator mediator = (Mediator) o;
        if ( mediator == null || mediator.getBanLogin() ){
            filterChain.doFilter(servletRequest, servletResponse);
        }else {
            if (uri.contains("/login")||uri.contains(".jpg")||uri.contains(".gif")||uri.contains(".png")||uri.contains(".css")||uri.contains("Login")||uri.contains("register")||uri.contains("Register")||uri.contains("index")||uri.contains("exit")||uri.contains("captcha")||uri.contains("check")){
                filterChain.doFilter(servletRequest, servletResponse);
            }else {
                HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
                httpServletResponse.sendRedirect("/myhouse/mediator/index");
            }
        }
    }
}
